//
//  JUFNLoginRegister.swift
//  WiBoPM
//
//  Created by 陈俊峰 on 2017/1/5.
//  Copyright © 2017年 陈俊峰. All rights reserved.
/*
 控件.先添加动画旋转圈
 cycleImageV
 
 半透明图片
 backImageV
 
 houseImageV
 
 gitLabel
 
 loginBtn
 
 registerBtn
 
 */

import UIKit
import SnapKit

@objc protocol JUFNLoginRegisterViewDelegate:NSObjectProtocol {
    
    @objc optional func userWillLogining()
    func userWillRegister()
    
}

class JUFNLoginRegister: UIView {
    weak var delegate:JUFNLoginRegisterViewDelegate?
    //提供一个方法,解决在子视图中更新视图空间
    func updateVisitor(imageName:String?,isMain:Bool = false,text:String?){
        if isMain {
            startAnimation()
        }else{
            
            houseImageV.isHidden = true
            backImageV.isHidden = true
            cycleImageV.image = UIImage(named: imageName!)
            gitLabel.text = text!
        }
        
        
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor.init(colorLiteralRed: 237.0/255, green: 237.0/255, blue: 237.0/255, alpha: 1)
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK 添加动画
    private func startAnimation(){
        let rotationAnomation : CABasicAnimation = CABasicAnimation(keyPath: "transform.rotation")
        rotationAnomation.toValue = M_PI * 2
        rotationAnomation.repeatCount = MAXFLOAT
        rotationAnomation.isRemovedOnCompletion = false
        rotationAnomation.duration = 15
        
        cycleImageV.layer.add(rotationAnomation, forKey: nil)
        
    }
    @objc private func loginBtnDidClick(){
        
        delegate?.userWillLogining!()
    }
    @objc private func registerBtnDidClick(){
        delegate?.userWillRegister()
    }
    private func setupUI() {
        //监听点击事件
        loginBtn.addTarget(self, action: #selector(loginBtnDidClick), for: .touchUpInside)
        registerBtn.addTarget(self, action: #selector(registerBtnDidClick), for: .touchUpInside)
        //添加控件,并设置约束
        addSubview(cycleImageV)
        addSubview(backImageV)
        addSubview(houseImageV)
        addSubview(gitLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        cycleImageV.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview().offset(-80)
        }
        
        backImageV.snp.makeConstraints { (make) in
            make.center.equalTo(cycleImageV)
        }
        
        houseImageV.snp.makeConstraints { (make) in
            make.center.equalTo(cycleImageV)
        }
        gitLabel.snp.makeConstraints { (make) in
            make.top.equalTo(cycleImageV.snp.bottom).offset(20)
            make.width.equalTo(220)
            make.centerX.equalTo(cycleImageV)
        }
        loginBtn.snp.makeConstraints { (make) in
            make.top.equalTo(gitLabel.snp.bottom).offset(20)
            make.width.equalTo(100)
            make.trailing.equalTo(cycleImageV.snp.centerX).offset(-20)
        }
        registerBtn.snp.makeConstraints { (make) in
            make.centerY.width.equalTo(loginBtn)
            make.leading.equalTo(cycleImageV.snp.centerX).offset(20)
        }
    }
    
    
    private lazy var cycleImageV:UIImageView = {
        let imageV = UIImageView()
        imageV.image = UIImage(named:"visitordiscover_feed_image_smallicon")
        return imageV
    }()
    
    private lazy var backImageV : UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_mask_smallicon"))
    
    private lazy var houseImageV = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_house"))
    
    private lazy var gitLabel = UILabel(text: "关注一些人,回到这里看看有什么惊喜", fontSize: 15, textColor: UIColor.darkGray)
    
    private lazy var loginBtn = UIButton(normalTitle: "登录", normalTitleColor: UIColor.darkGray, backgroundImage: UIImage(named: "common_button_white")!)
    private lazy var registerBtn = UIButton(normalTitle: "注册", normalTitleColor: UIColor.darkGray, backgroundImage: UIImage(named: "common_button_white")!)
    
    
}
